package com.ruoyi.web.controller.hz;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Do.HzCommentDo;
import com.ruoyi.system.domain.Do.HzFileUrlDo;
import com.ruoyi.system.domain.HzUploadInfo;
import com.ruoyi.system.domain.co.HzCommentCo;
import com.ruoyi.system.domain.co.HzUploadInfoCo;
import com.ruoyi.system.service.IHzCommentService;
import com.ruoyi.system.service.IHzUploadInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Video")
public class VideoController extends BaseController {

    private String prefix = "browse/";

    @Autowired
    private IHzUploadInfoService hzUploadInfoService;
    @Autowired
    private IHzCommentService hzCommentService;


    /**
     * 标题查询查找数据
     *
     * @param id
     * @param titleName
     * @return TableDataInfo
     */
    @PostMapping("/browse/findSearch")
    @ResponseBody
    public TableDataInfo findSearch(String id, String titleName, Integer pageNum) {
        startPage(pageNum, 16);
        HzUploadInfoCo hzUploadInfo = new HzUploadInfoCo();
        if (!id.isEmpty()) {
            hzUploadInfo.setFileClassDictId(id);   //首页默认首页没有id
        }
        hzUploadInfo.setTitleName(titleName);
        List<HzUploadInfo> fileInfoList = hzUploadInfoService.selectHzUploadInfoList(hzUploadInfo);
        return getDataTable(fileInfoList);
    }

    /**
     * 根据 fileClassDictId  跳转不同的首页推荐
     *
     * @param id   字典文件类型id
     * @param mmap
     * @return
     */
    @GetMapping("/browse/{id}")
    public String main(@PathVariable("id") String id, ModelMap mmap) {
        startPage(1, 16);
        HzUploadInfoCo hzUploadInfo = new HzUploadInfoCo();
        hzUploadInfo.setFileClassDictId(id);
        List<HzUploadInfo> fileInfoList = hzUploadInfoService.selectHzUploadInfoList(hzUploadInfo);
        mmap.put("TableDataInfo", getDataTable(fileInfoList));
        mmap.put("fileClassDictId", id);
        return prefix + "main";
    }


    /**
     * 播放视频页面
     *
     * @param id   上传信息id
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{id}")
    public String playVideo(@PathVariable("id") Long id, ModelMap mmap) {
        try {
            SysUser user = ShiroUtils.getSysUser();
            mmap.put("user", user);
            //展示评论内容
            HzCommentCo comment = new HzCommentCo();
            comment.setUploadInfoId(id);
            comment.setSelectCount(20);
            List<HzCommentDo> commentList = hzCommentService.CommentList(comment);
            mmap.put("commentList", commentList);
            //推荐内容信息
            startPage(1, 8);
            HzUploadInfoCo hzUploadInfo = new HzUploadInfoCo();
            hzUploadInfo.setFileType(2);  //视频
            List<HzUploadInfo> fileInfoList = hzUploadInfoService.selectHzUploadInfoList(hzUploadInfo);
            mmap.put("fileInfoList", fileInfoList);
            //当前点播信息
            HzUploadInfo uploadInfo = hzUploadInfoService.selectHzUploadInfoById(id);
            mmap.put("uploadInfo", uploadInfo);
            List<Object> fileUrlList = hzUploadInfoService.playVideoBYid(id);
            if (uploadInfo.getFileType() == 1) {
                mmap.put("fileUrlList", fileUrlList);
                return prefix + "ImgView";
            } else if (uploadInfo.getFileType() == 2) {
                HzFileUrlDo fileUrlDo = (HzFileUrlDo) fileUrlList.get(0);
                mmap.put("srsUrl", fileUrlDo.getSrsUrl());
                mmap.put("fmUrl", uploadInfo.getSurfacePlotUrl());
                return prefix + "playVideo";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
